package com.coocaa.ops.admin.v1.audit.provider.impl;


import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.v1.audit.model.CheckInfoModel;
import com.coocaa.ops.admin.v1.audit.provider.AbstractAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.homepage.biz.impl.HomepageInfoBizImpl;
import com.coocaa.ops.admin.v1.homepage.service.impl.HomepageInfoQueryServiceImpl;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.homepage.entity.HomepageInfoEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @date : 2018/9/26.
 * @description: 审核前校验-审核-撤销审核-上架操作 提供者接口实现-首页
 */
@Service
public class HomeAuditTaskProviderImpl extends AbstractAuditTaskProviderImpl<HomepageInfoEntity> {
    @Autowired
    private HomepageInfoBizImpl homeBiz;
    @Autowired
    private HomepageInfoQueryServiceImpl homeInfoQueryService;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return homeBiz;
    }

    /**
     * 审核前的校验
     *
     * @param resourceBaseModel
     * @return
     */
    @Override
    public CheckInfoModel checkBeforeAudit(ResourceBaseModel resourceBaseModel) {
        HomepageInfoEntity homepageInfoEntity = homeInfoQueryService.getDetailInfo(resourceBaseModel.getId(), resourceBaseModel.getVersion());
        if (homepageInfoEntity != null) {
            List<HomeModuleContentConfigInfo> moduleList = homepageInfoEntity.getModuleList();
            for (HomeModuleContentConfigInfo homeModuleContentConfigInfo : moduleList) {
                if (Objects.equals(1, homeModuleContentConfigInfo.getModuleIsDmp())) {
                    List<HomeModuleContentConfigInfo> dmpModuleList = homeModuleContentConfigInfo.getDmpModuleList();
                    if (CollectionUtils.isNotEmpty(dmpModuleList)) {
                        for (HomeModuleContentConfigInfo dmpModule : dmpModuleList) {
                            if (ResourceStatusValidator.isNotOnline(dmpModule.getStatus())) {
                                //存在没有审核通过的板块
                                return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(dmpModule));
                            }
                        }
                    }
                } else {
                    if (ResourceStatusValidator.isNotOnline(homeModuleContentConfigInfo.getStatus())) {
                        //存在没有审核通过的板块
                        return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(homeModuleContentConfigInfo));
                    }
                }
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    /**
     * 获取校验信息-首页中的版面
     *
     * @param homeModuleContentConfigInfo
     * @return
     */
    private String getCheckMsg(HomeModuleContentConfigInfo homeModuleContentConfigInfo) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("moduleID: [")
                .append(homeModuleContentConfigInfo.getId())
                .append("],")
                .append("moduleName： ")
                .append(homeModuleContentConfigInfo.getModuleName())
        ;
        return stringBuilder.toString();
    }
}
